package taboo

import (
	"fmt"
	"sync"
)

type treeCache struct {
	sync.RWMutex
	treeDict map[string]*MatchTree
}

var tree_cache = &treeCache{
	treeDict: make(map[string]*MatchTree),
}

func SetCacheTree(name string, tree *MatchTree) error {
	if name == `` {
		return fmt.Errorf(`SetCacheTree must have a valid name. Otherwise use SetDefaultTree`)
	}
	tree_cache.Lock()
	defer tree_cache.Unlock()
	tree_cache.treeDict[name] = tree
	return nil
}

func GetCacheTree(name string) (*MatchTree, bool) {
	tree_cache.RLock()
	defer tree_cache.RUnlock()
	tree, ok := tree_cache.treeDict[name]
	return tree, ok
}

func SetDefaultTree(tree *MatchTree) {
	tree_cache.Lock()
	defer tree_cache.Unlock()
	tree_cache.treeDict[``] = tree
}

func GetDefaultTree() *MatchTree {
	tree_cache.RLock()
	defer tree_cache.RUnlock()
	tree := tree_cache.treeDict[``]
	return tree
}
